All files / src/app/api/wishlist/[id] route.ts

100% Statements 39/39
100% Branches 5/5
100% Functions 1/1
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x  
import { NextRequest, NextResponse } from 'next/server';
import {
  withAuth,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";
import { prisma } from "@/lib/prisma";
import { Session } from "next-auth";
 
// DELETE /api/wishlist/[id] - Remove item from wishlist
async function handleDelete(
  _request: NextRequest,
  context: RouteContext | undefined,
  session: Session
): Promise<NextResponse<ApiSuccessResponse<{ message: string }>>> {
  const { id } = await context!.params!;
  const productId = parseInt(id);
  const userId = session.user.id;
 
  // Find and delete wishlist item
  const wishlistItem = await prisma.wishlist.findUnique({
    where: {
      userId_productId: {
        userId,
        productId}}});
 
  if (!wishlistItem) {
    throw ApiError.notFound("Wishlist item");
  }
 
  await prisma.wishlist.delete({
    where: { id: wishlistItem.id }});
 
  return successResponse({ message: "Wishlist item removed successfully" });
}
 
export const DELETE = withErrorHandling(withAuth(handleDelete));